//
//  AY_BookSubSortPage.m
//  AiYue
//
//  Created by boWen on 2023/10/25.
//

#import "AY_BookSubSortPage.h"
#import "AY_BookSortCell.h"
#import "AY_BookSubDetailPage.h"

@interface AY_BookSubSortPage ()<UICollectionViewDelegate,UICollectionViewDataSource,SXLoadingDelegate>

@property (strong,nonatomic) UICollectionView *collectionView;
@property (strong,nonatomic) NSArray *dataSourceAry;
@property (strong,nonatomic) UIView *searchView;
@property (strong,nonatomic) AY_LoadingView *loadingView;
@end

@implementation AY_BookSubSortPage

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self barStyleBlack];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWithBaseUI];
    [self getConfigNet];
}


- (void)initWithBaseUI {
    self.gk_navigationBar.hidden = YES;
    [self.view addSubview:self.collectionView];
}

- (void)getConfigNet {
    [self.loadingView loadingInView:self.view];
    NSString * type = [NSString stringWithFormat:@"%ld", self.indx];
    [AYApiRequest getArticleCategoryListType:type Success:^(AY_SortModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            [self.loadingView dismiss];
            self.dataSourceAry = pkg.data;
        }else {
            [self.loadingView refresh];
        }
        [self.collectionView reloadData];
    } failure:^(int status, NSString * _Nonnull info) {
        [self.loadingView refresh];
    }];
}



#pragma mark -- UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSourceAry.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookSortCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AY_BookSortCell" forIndexPath:indexPath];
    if(self.dataSourceAry.count > indexPath.row) {
        cell.sxSortModel = self.dataSourceAry[indexPath.row];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookSubDetailPage *vc = [[AY_BookSubDetailPage alloc] init];
    if(self.dataSourceAry.count > indexPath.row) {
        vc.categoaryId = [self.dataSourceAry[indexPath.row] cId];
        vc.categoaryName = [self.dataSourceAry[indexPath.row] name];
    }
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - init

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake((SCREEN_W - YNUM(48))/2, YNUM(68));
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16);
        layout.minimumLineSpacing = 16;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, YNUM(16), SCREEN_W, SCREEN_H - GK_STATUSBAR_HEIGHT - GK_TABBAR_HEIGHT - GK_SAFEAREA_TOP - YNUM(16)) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = COLOR_Bg;
        _collectionView.delegate = self;
        [_collectionView registerClass:[AY_BookSortCell class] forCellWithReuseIdentifier:@"AY_BookSortCell"];
    }
    return _collectionView;
}

- (AY_LoadingView *)loadingView {
    if (!_loadingView) {
        _loadingView = [[AY_LoadingView alloc] initWithFrame:CGRectMake(0, YNUM(16), SCREEN_W, SCREEN_H - GK_STATUSBAR_HEIGHT - GK_TABBAR_HEIGHT - GK_SAFEAREA_TOP - YNUM(16))];
        _loadingView.backgroundColor = COLOR(@"#F7F5F6");
        _loadingView.delegate = self;
        _loadingView.hideLeftpop = YES;
    }
    return _loadingView;
}

- (void)refreshAction {
    [self getConfigNet];
}

- (NSArray *)dataSourceAry {
    if (!_dataSourceAry) {
        _dataSourceAry = [NSArray array];
    }
    return _dataSourceAry;
}

@end
